﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lizk.SimpleHUD;
using SharpMedia.Input;
using System.Xml.Serialization;
using Lizk.Utilities;
using System.IO;
using System.Windows.Forms;

namespace SharpMedia
{
    [XmlRoot]
    public class InputSettings
    {
        public List<KeyboardBinding<HUDCommands>> Bindings { get; set; }
        public InputSettings()
        {
            Bindings = new List<KeyboardBinding<HUDCommands>>();
        }

        private void AddDefaultBindings()
        {
            Bindings.Add(new KeyboardBinding<HUDCommands>(HUDCommands.Up) { Primary = Keys.Up });
            Bindings.Add(new KeyboardBinding<HUDCommands>(HUDCommands.Down) { Primary = Keys.Down });
            Bindings.Add(new KeyboardBinding<HUDCommands>(HUDCommands.Left) { Primary = Keys.Left });
            Bindings.Add(new KeyboardBinding<HUDCommands>(HUDCommands.Right) { Primary = Keys.Right });
            Bindings.Add(new KeyboardBinding<HUDCommands>(HUDCommands.Back) { Primary = Keys.Back });
            Bindings.Add(new KeyboardBinding<HUDCommands>(HUDCommands.Select) { Primary = Keys.Enter });
            Bindings.Add(new KeyboardBinding<HUDCommands>(HUDCommands.Fullscreen) { Primary = Keys.Enter, PrimaryMods = Keys.Alt });
            Bindings.Add(new KeyboardBinding<HUDCommands>(HUDCommands.PageDown) { Primary = Keys.Next });
            Bindings.Add(new KeyboardBinding<HUDCommands>(HUDCommands.PageUp) { Primary = Keys.Prior });
            Bindings.Add(new KeyboardBinding<HUDCommands>(HUDCommands.Menu) { Primary = Keys.Space });
            Bindings.Add(new KeyboardBinding<HUDCommands>(HUDCommands.UpdateDatabase) { Primary = Keys.F9 });
            Bindings.Add(new KeyboardBinding<HUDCommands>(HUDCommands.Web) { Primary = Keys.F11 });
            Bindings.Add(new KeyboardBinding<HUDCommands>(HUDCommands.Preset1) { Primary = Keys.F1 });
            Bindings.Add(new KeyboardBinding<HUDCommands>(HUDCommands.Preset2) { Primary = Keys.F2 });
            Bindings.Add(new KeyboardBinding<HUDCommands>(HUDCommands.Preset3) { Primary = Keys.F3 });
            Bindings.Add(new KeyboardBinding<HUDCommands>(HUDCommands.Play));
            Bindings.Add(new KeyboardBinding<HUDCommands>(HUDCommands.Pause));
            Bindings.Add(new KeyboardBinding<HUDCommands>(HUDCommands.PlayPause) { Primary = Keys.MediaPlayPause });
            Bindings.Add(new KeyboardBinding<HUDCommands>(HUDCommands.Stop) { Primary = Keys.MediaStop });
            Bindings.Add(new KeyboardBinding<HUDCommands>(HUDCommands.Previous) { Primary = Keys.MediaPreviousTrack });
            Bindings.Add(new KeyboardBinding<HUDCommands>(HUDCommands.Next) { Primary = Keys.MediaNextTrack });
            Bindings.Add(new KeyboardBinding<HUDCommands>(HUDCommands.Forward) { Primary = Keys.Right });
            Bindings.Add(new KeyboardBinding<HUDCommands>(HUDCommands.Rewind) { Primary = Keys.Left });
        }


        public static bool TryLoadSettings(string file, out InputSettings settings)
        {
            if (File.Exists(file))
            {
                try
                {
                    settings = XmlSettings.loadXml<InputSettings>(file);
                    return true;
                }
                catch
                {
                }
            }
            settings = new InputSettings(); settings.AddDefaultBindings();
            return false;
        }

    }
}
